<?php namespace Admin\Controller; 
//货品控制器

//商品属性表
use Common\Model\GoodsAttr;
//货品列表
use Common\Model\GoodsList;
class GoodsListController extends CommonController{
//  首页
    public function index(){
    	 
    	$goodsListModel = new GoodsList();
    	//判断如果有修改的话，执行操作
		if(IS_POST){
//			修改数据库
			$goodsListModel->store();
//			p(Q("post."));
		}
    	$tid = Q("get.tid",0,'intval');
		$aid = Q("get.aid",0,'intval');
		//处理表格表头的规格的数据例如:"颜色、尺码"
		$goodsAttrModel = new GoodsAttr();
		$data = $goodsAttrModel
				->join('type_attr','type_attr_taid','=','type_tid')
				->where("type_tid={$tid} AND class=1")
				 ->groupBy('taid')
				->get();


		//添加货品的时候下拉菜单所选择的内容(一定是从商品属性表获得)		
		foreach ($data as $k => $v) {
			$data[$k]['value'] =$goodsAttrModel->where("type_attr_taid={$v['taid']} AND goods_aid={$aid}")->get();
		}
//		p($data);
		View::with('data',$data);
		
		//显示货品
		
		$goodListData = $goodsListModel->where("goods_aid={$aid}")->get();
		foreach ($goodListData as $k => $value) {
			$goodListData[$k]['value']=$goodsAttrModel->where("gtid IN({$value['combine']})")->get();
		}
//		p($goodListData);
		View::with('goodListData',$goodListData);
        View::make();
    }
//  编辑
    public function edit(){
    	$goodsListModel = new GoodsList();
		
		if(IS_POST){
			$tid = Q("get.tid",0,'intval');
			$aid = Q('get.aid',0,'intval');
			$goodsListModel->edit();
			View::success("添加成功",U('index',array('tid'=>$tid,'aid'=>$aid)));
		}	
			
//  	p(Q("get."));
//  	获得旧数据
		$glid = Q("get.glid",0,'intval');
		$goodsListData = $goodsListModel->where("glid={$glid}")->find();
		$spce = $goodsListModel->where("glid={$glid}")->lists('combine');
		$spce = explode(',', $spce[0]);
//		p($spce);
		View::with('goodsListData',$goodsListData);
		//处理表格表头的规格的数据例如:"颜色、尺码"
		$aid = Q("get.aid",0,'intval');
		$tid = Q("get.tid",0,'intval');
		$goodsAttrModel = new GoodsAttr();
		$data = $goodsAttrModel
				->join('type_attr','type_attr_taid','=','taid')
				->where("type_tid={$tid} AND class=1")
				->groupBy('taid')
				->get();
		
		foreach ($data as $k => $v) {
			$data[$k]['value'] =$goodsAttrModel->where("type_attr_taid={$v['taid']} AND goods_aid={$aid}")->get();
		}
		foreach ($spce as $k => $v) {
			$data[$k]['judes'] = $v;
		}		
//		p($data);
		View::with('data',$data);
        View::make();
    }


//  删除
    public function del(){
       $glid = Q("get.glid",0,'intval');
	   $goodsListModel = new GoodsList();
	   $goodsListData = $goodsListModel->where("glid={$glid}")->delete();
	   View::success("删除成功");
    }
}